package cn.hlife.com.hlfhaemocyte.utils;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import android.graphics.Bitmap;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

public class RDPrinterInterface {
    private static final String TAG = "RDPrinterInterface";
    public static final int RESULT_OPEN_SUCCESS = 1000;
    public static final int RESULT_OPEN_FAIL = 1001;
    public static final int RESULT_ERRO_CLOSE = 1002;
    public static final int RESULT_ERRO_SENDCMD = 1003;
    public static final byte[] INIT = getByteArray(new int[]{27, 64});
    public static final byte[] READ_REBACK = getByteArray(new int[]{27, 118});
    public static final byte[] ENTER_PRINT = getByteArray(new int[]{13});
    public static final byte[] WRAP_PRINT = getByteArray(new int[]{10});
    public static final byte[] LEFT = getByteArray(new int[]{27, 97, 0});
    public static final byte[] MIND = getByteArray(new int[]{27, 97, 1});
    public static final byte[] RIGHT = getByteArray(new int[]{27, 97, 2});
    public static final byte[] LARGE_TWICE = getByteArray(new int[]{27, 88, 2});
    public static final byte[] LINE_FEED = getByteArray(new int[]{27, 100, 1});
    public static final byte[] LINE_FEED4 = getByteArray(new int[]{27, 100, 6});
    public static final byte[] LINE_FEED8 = getByteArray(new int[]{27, 100, 8});
    public static final byte[] BAUD_RATE_U = getByteArray(new int[]{27, 188, 0});
    public static final byte[] BAUD_RATE_P = getByteArray(new int[]{234, 11});
    public static final byte[] CODE_BOTTOM = getByteArray(new int[]{27, 77, 2});
    public static final byte[] CODE_ENA13 = getByteArray(new int[]{29, 107, 67, 13});
    public static final byte[] GET_STATE = getByteArray(new int[]{27, 118});
    public static final byte[] IMAGECMD = getByteArray(new int[]{0, 27, 42, 1});
    public static final byte[] IMAGECMD24 = getByteArray(new int[]{0, 27, 42, 33});
    public static final byte[] CHROMA_SET = getByteArray(new int[]{27, 115, 43});
    public static final byte[] TXT_CHROMA_DEF = getByteArray(new int[]{27, 115, 43, 50});
    public static final byte[] IMG_CHROMA_DEF = getByteArray(new int[]{27, 115, 43, 22});
    public static final byte[] PRINT_LINESPACE = getByteArray(new int[]{27, 49});
    public static final byte[] RD_TAG_TEST = getByteArray(new int[]{27, 160, 13, 32, 32, 32, 32, 32, 32, 179, 198, 214, 216, 181, 165, 13, 10, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 13, 10, 50, 48, 49, 50, 45, 49, 48, 45, 49, 49, 32, 49, 50, 58, 51, 52, 58, 52, 53, 13, 10, 195, 171, 214, 216, 58, 50, 48, 32, 75, 103, 13, 10, 198, 164, 214, 216, 58, 32, 53, 32, 75, 103, 13, 10, 190, 187, 214, 216, 58, 49, 53, 32, 75, 103, 13, 10, 45, 13, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 13, 10, 27, 130});
    public static final int BLACK = -16777216;
    public static final int WITER = -1;
    public static RDPrinterInterface mInstance = null;

    public RDPrinterInterface() {
    }

    public static RDPrinterInterface getInstance() {
        if(mInstance == null) {
            mInstance = new RDPrinterInterface();
        }

        return mInstance;
    }

    public static final byte[] getByteArray(int... array) {
        byte[] bytes = new byte[array.length];

        for(int i = 0; i < array.length; ++i) {
            bytes[i] = (byte)array[i];
        }

        return bytes;
    }

    public void writeSdcard(String input) {
        File file = new File("sdcard/Download/printdata.txt");

        try {
            if(!file.exists()) {
                file.createNewFile();
            }

            BufferedWriter e = new BufferedWriter(new FileWriter(file, true));
            e.write(input + "\n");
            e.flush();
            e.close();
        } catch (IOException var4) {
            var4.printStackTrace();
            Log.e("RDPrinterInterface", "input = " + input, var4);
        }

    }

    public String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if(src != null && src.length > 0) {
            for(int i = 0; i < src.length; ++i) {
                int v = src[i] & 255;
                String hv = Integer.toHexString(v);
                if(hv.length() < 2) {
                    stringBuilder.append(0);
                }

                stringBuilder.append(hv).append(" ");
            }

            return stringBuilder.toString().toUpperCase();
        } else {
            return null;
        }
    }

    public int[] getBitmapData(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        for(int i = 0; i < pixels.length; ++i) {
            if(pixels[i] == -1) {
                pixels[i] = 0;
            } else {
                pixels[i] = 1;
            }
        }

        return this.get8BitData(pixels, width, height);
    }

    public int binaryToDecimal(int[] src) {
        int result = 0;

        for(int i = 0; i < src.length; ++i) {
            result = (int)((double)result + (double)src[i] * Math.pow(2.0D, (double)(src.length - i - 1)));
        }

        return result;
    }

    public int[] get8BitData(int[] source, int width, int height) {
        int[] targData = new int[width * height / 8];

        for(int i = 0; i < height / 8; ++i) {
            for(int j = 0; j < width; ++j) {
                int[] temp = new int[8];

                for(int k = 0; k < 8; ++k) {
                    temp[k] = source[(k + i * 8) * width + j];
                }

                targData[i * width + j] = this.binaryToDecimal(temp);
            }
        }

        return targData;
    }

    public int[] getBitmapData24(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        for(int i = 0; i < pixels.length; ++i) {
            if(pixels[i] == -1) {
                pixels[i] = 0;
            } else {
                pixels[i] = 1;
            }
        }

        return this.get24BitData(pixels, width, height);
    }

    public int[] get24BitData(int[] source, int width, int height) {
        int[] target = new int[width * height / 8];

        for(int i = 0; i < height / 24; ++i) {
            for(int j = 0; j < width; ++j) {
                for(int k = 0; k < 3; ++k) {
                    int[] temp = new int[8];

                    for(int m = 0; m < 8; ++m) {
                        temp[m] = source[(i * 24 + k * 8 + m) * width + j];
                    }

                    target[3 * (i * width + j) + k] = this.binaryToDecimal(temp);
                }
            }
        }

        return target;
    }

    public byte[] getImageCmd(byte[] CMD, int width) {
        String[] result = new String[2];
        String str = Integer.toHexString(width).toUpperCase();
        StringBuffer sbuffer = new StringBuffer();
        int olen = 4 - str.length();

        for(int end = 0; end < olen; ++end) {
            sbuffer.append("0");
        }

        sbuffer.append(str);
        result[0] = sbuffer.toString().substring(2, 4);
        result[1] = sbuffer.toString().substring(0, 2);
        int[] var8 = new int[]{Integer.parseInt(result[0], 16), Integer.parseInt(result[1], 16)};
        return concat(CMD, getByteArray(var8));
    }

    public static byte[] concat(byte[] first, byte[] second) {
        byte[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }

    public int[] getCacheData(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int[] pixels = new int[width * height];
        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        for(int target = 0; target < pixels.length; ++target) {
            if(pixels[target] == -1) {
                pixels[target] = 0;
            } else {
                pixels[target] = 1;
            }
        }

        int[] var10 = new int[width * height / 8];

        for(int i = 0; i < height / 8; ++i) {
            for(int j = 0; j < width; ++j) {
                int[] temp = new int[8];

                for(int m = 0; m < 8; ++m) {
                    temp[m] = pixels[(i * 8 + m) * width + j];
                }

                var10[j * height / 8 + i] = this.binaryToDecimal(temp);
            }
        }

        return var10;
    }
}

